home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1993 / Internet Info CD-ROM (Walnut Creek) (1993).iso / networking / cisco / gwacc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-07  |  8.1 KB  |  309 lines

  1. /***********************************************************
  2.     Copyright 1989 by Carnegie Mellon University
  3.  
  4.                       All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of CMU not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21. ******************************************************************/
  22. /*
  23.  * Copyright (c) 1983,1988 Regents of the University of California.
  24.  * All rights reserved.
  25.  *
  26.  * Redistribution and use in source and binary forms are permitted
  27.  * provided that this notice is preserved and that due credit is given
  28.  * to the University of California at Berkeley. The name of the University
  29.  * may not be used to endorse or promote products derived from this
  30.  * software without specific prior written permission. This software
  31.  * is provided ``as is'' without express or implied warranty.
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include <strings.h>
  36. #include <ctype.h>
  37.  
  38. #include <sys/param.h>
  39. #include <sys/socket.h>
  40.  
  41. #include <netinet/in.h>
  42.  
  43. #include <netdb.h>
  44. #include "asn1.h"
  45. #include "snmp.h"
  46. #include "snmp_impl.h"
  47. #include "snmp_api.h"
  48. #include "snmp_client.h"
  49. #include "mib.h"
  50.  
  51. extern    char *routename(), *plural();
  52. extern    char *malloc();
  53. int nflag;
  54.  
  55.  
  56. struct acc_entry {
  57.     oid        instance[8];
  58.     struct in_addr  source, destination;
  59.     int        set_source, set_destination;
  60.     int        packets;
  61.     int        set_packets;
  62.     int        bytes;
  63.     int        set_bytes;
  64. };
  65.  
  66.  
  67. int     snmp_dump_packet = 0;
  68.  
  69. #define ACCSRC        1
  70. #define ACCDEST        2
  71. #define ACCPKTS        3
  72. #define ACCBYTS        4
  73. #define ACCCKPT        9
  74. static oid oid_acctable[] = {1, 3, 6, 1, 4, 1, 9, 2, 4, 7};
  75. static oid oid_accsrc[] =   {1, 3, 6, 1, 4, 1, 9, 2, 4, 7, 1, 1};
  76. static oid oid_accdest[] =  {1, 3, 6, 1, 4, 1, 9, 2, 4, 7, 1, 2};
  77. static oid oid_accpkts[] =  {1, 3, 6, 1, 4, 1, 9, 2, 4, 7, 1, 3};
  78. static oid oid_accbyts[] =  {1, 3, 6, 1, 4, 1, 9, 2, 4, 7, 1, 4};
  79.  
  80. #define TABLE_INDEX        ((sizeof(oid_acctable)/sizeof(oid)) - 1)
  81.  
  82. /*
  83.  * Print IP accounting tables.
  84.  */
  85. main(argc, argv)
  86.     int argc;
  87.     char *argv[];
  88. {
  89.     struct snmp_session session, *ss;
  90.     struct acc_entry acc, *ap = &acc;
  91.     struct snmp_pdu *request, *response;
  92.     struct variable_list *vp;
  93.     char name[16], *flags, *gateway = NULL, *community = NULL;
  94.     oid *instance, type;
  95.     int toloopback, status, arg, count, heading_done = 0;
  96.     char ch;
  97.  
  98.     init_mib(); 
  99.  
  100.     /* gwacc -c -n -d <gw> <community>
  101.  
  102.     -c    show checkpoint accounting table
  103.     -n    don't convert IP addresses to names
  104.     -d    dump SNMP packets */
  105.  
  106.     nflag = 0;
  107.     for (arg = 1; arg < argc; arg++){
  108.         if (argv[arg][0] == '-'){
  109.  
  110.             switch(argv[arg][1]){
  111.             case 'c':
  112.                 oid_acctable[TABLE_INDEX] = ACCCKPT;
  113.                 oid_accsrc[TABLE_INDEX] = ACCCKPT;
  114.                 oid_accdest[TABLE_INDEX] = ACCCKPT;
  115.                 oid_accpkts[TABLE_INDEX] = ACCCKPT;
  116.                 oid_accbyts[TABLE_INDEX] = ACCCKPT;
  117.                 break;
  118.             case 'd':
  119.                 snmp_dump_packet++;
  120.                 break;
  121.             case 'n':
  122.                 nflag++;
  123.                 break;
  124.             default:
  125.                 printf("invalid option: -%c\n", argv[arg][1]);
  126.                 break;
  127.             }
  128.         continue;
  129.             }
  130.  
  131.         if (gateway == NULL){
  132.             gateway = argv[arg];
  133.             } else if (community == NULL){
  134.                     community = argv[arg]; 
  135.                } else {
  136.                printf("usage: gwacc [-c|-n|-d] <gw> <community>\n");
  137.             exit(1);
  138.                }
  139.     }
  140.     
  141.     if (!gateway){
  142.         printf("usage: gwacc [-c|-n|-d] <gw> <community>\n");
  143.         exit(1);
  144.     }
  145.  
  146.     if (!community) community = "public";
  147.  
  148.     bzero((char *)&session, sizeof(struct snmp_session));
  149.     session.peername = gateway;
  150.     session.community = (u_char *)community;
  151.     session.community_len = strlen((char *)community);
  152.     session.retries = SNMP_DEFAULT_RETRIES;
  153.     session.timeout = SNMP_DEFAULT_TIMEOUT;
  154.     session.authenticator = NULL;
  155.     snmp_synch_setup(&session);
  156.     ss = snmp_open(&session);
  157.     if (ss == NULL){
  158.         printf("Couldn't open snmp\n");
  159.         exit(-1);
  160.     }
  161.  
  162.     request = snmp_pdu_create(GETNEXT_REQ_MSG);
  163.  
  164.     snmp_add_null_var(request, oid_accsrc, sizeof(oid_accsrc)/sizeof(oid));
  165.     snmp_add_null_var(request, oid_accdest, sizeof(oid_accdest)/sizeof(oid));
  166.     snmp_add_null_var(request, oid_accpkts, sizeof(oid_accpkts)/sizeof(oid));
  167.     snmp_add_null_var(request, oid_accbyts, sizeof(oid_accbyts)/sizeof(oid));
  168.  
  169.     while(request){
  170.         status = snmp_synch_response(ss, request, &response);
  171.         if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR){
  172.         if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOSUCHNAME) {
  173.             printf("This name does not exist: ");
  174.             for (count=1, vp = response->variables;
  175.             vp && count != response->errindex; vp= vp->next_variable, count++);
  176.                 if (vp) print_objid(vp->name, vp->name_length);
  177.             printf("\n");
  178.                 }
  179.  
  180.         fprintf(stderr, "SNMP request failed\n");
  181.         break;
  182.         }
  183.         instance = NULL;
  184.         request = NULL;
  185.         ap->set_destination = 0;
  186.         ap->set_source = 0;
  187.         ap->set_packets = 0;
  188.         ap->set_bytes = 0;
  189.         for(vp = response->variables; vp; vp = vp->next_variable){
  190.  
  191.         if (vp->name_length != 20 ||
  192.             bcmp((char *)vp->name, (char *)oid_acctable, sizeof(oid_acctable))){
  193.             continue;    /* if it isn't in this subtree, just continue */
  194.         }
  195.  
  196.         if (instance != NULL){
  197.             oid *ip, *op;
  198.             int count;
  199.  
  200.             ip = instance;
  201.             op = vp->name + 12;
  202.             for(count = 0; count < 8; count++){
  203.             if (*ip++ != *op++)
  204.                 break;
  205.             }
  206.             if (count < 8)
  207.             continue;    /* not the right instance, ignore */
  208.         } else {
  209.             instance = vp->name + 12;
  210.         }
  211.         /*
  212.          * At this point, this variable is known to be in the routing table
  213.          * subtree, and is of the right instance for this transaction.
  214.          */
  215.  
  216.         if (request == NULL)
  217.             request = snmp_pdu_create(GETNEXT_REQ_MSG);
  218.         snmp_add_null_var(request, vp->name, vp->name_length);
  219.  
  220.         type = vp->name[11];
  221.         switch ((char)type){
  222.             case ACCDEST:
  223.             bcopy((char *)vp->val.string, (char *)&ap->destination, sizeof(u_long));
  224.             ap->set_destination = 1;
  225.             break;
  226.             case ACCSRC:
  227.             bcopy((char *)vp->val.string, (char *)&ap->source, sizeof(u_long));
  228.             ap->set_source = 1;
  229.             break;
  230.             case ACCPKTS:
  231.             ap->packets = *vp->val.integer;
  232.             ap->set_packets = 1;
  233.             break;
  234.             case ACCBYTS:
  235.             ap->bytes = *vp->val.integer;
  236.             ap->set_bytes = 1;
  237.             break;
  238.         }
  239.         }
  240.  
  241.         if (!(ap->set_destination && ap->set_source
  242.         && ap->set_packets && ap->set_bytes)){
  243.             if (request) {
  244.                     snmp_free_pdu(request);
  245.                 request = 0;
  246.                 continue;
  247.             } else {
  248.                 printf("No accounting table or table empty\n");
  249.                 break;
  250.             }
  251.             /* request = 0;
  252.             continue; */
  253.         }
  254.     
  255.         if (!(heading_done)) { 
  256.         printf("%-25.25s %-30.30s  %6s %6s\n",
  257.             "Source", "Destination",
  258.             "Packets", "Bytes");
  259.         heading_done = 1;
  260.         }
  261.  
  262.         printf("%-25.25s ", routename(ap->source));
  263.         printf("%-30.30s  ", routename(ap->destination));
  264.         printf("%6d ", ap->packets);
  265.             printf(" %6d\n", ap->bytes);
  266.  
  267.     }
  268. }
  269.  
  270. char *routename(in)
  271.     struct in_addr in;
  272. {
  273.     register char *cp;
  274.     static char line[MAXHOSTNAMELEN + 1];
  275.     struct hostent *hp;
  276.     static char domain[MAXHOSTNAMELEN + 1];
  277.     static int first = 1;
  278.     char *index();
  279.  
  280.     if (first) {
  281.         first = 0;
  282.         if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
  283.             (cp = index(domain, '.')))
  284.             (void) strcpy(domain, cp + 1);
  285.         else
  286.             domain[0] = 0;
  287.     }
  288.     cp = 0;
  289.     if (!nflag) {
  290.         hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
  291.             AF_INET);
  292.         if (hp) {
  293.             if ((cp = index(hp->h_name, '.')) &&
  294.                 !strcmp(cp + 1, domain))
  295.                 *cp = 0;
  296.             cp = hp->h_name;
  297.         }
  298.     }
  299.     if (cp)
  300.         strncpy(line, cp, sizeof(line) - 1);
  301.     else {
  302. #define C(x)    ((x) & 0xff)
  303.         in.s_addr = ntohl(in.s_addr);
  304.         sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
  305.             C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
  306.     }
  307.     return (line);
  308. }
  309.